package com.itheima.edu.info.manager.B_Controller;

import com.itheima.edu.info.manager.C_Service.StudentService;
import com.itheima.edu.info.manager.E_domain.Student;

import java.util.ArrayList;
import java.util.Scanner;

public abstract class BaseStudentController {
    StudentService studentService = new StudentService();
    Scanner sc = new Scanner(System.in);

    public final void start() {
        studentLoop:
        while (true) {
            System.out.println("--------欢迎来到《学生》管理系统--------");
            System.out.println("请输入您的选择：1.添加学生  2.删除学生  3.修改学生 4.查看学生 5.退出");
            String chose = sc.next();
            switch (chose) {
                case "1":
                    addStudent();
                    break;
                case "2":
                    deleteStudent();
                    break;
                case "3":
                    updateStudent();
                    break;
                case "4":
                    searchStudent();
                    break;
                case "5":
                    System.out.println("感谢您使用学生管理系统，再见");
                    break studentLoop;
                default:
                    System.out.println("您的输入有误，请重新输入");
                    break;
            }
        }
    }

    public final void addStudent() {
        String id;
        int age;

        while (true) {
            //键盘录入信息：
            System.out.println("请输入学生id：");
            id = sc.next();
            //判断：
            boolean exists = studentService.isExists(id);
            if (exists) {
                System.out.println("不好意思，学号已被占用，请重新录入");
            } else {
                break;
            }
        }
        Student student = inputStudentInfo(id);

        //将封装好的信息传给业务员进行逻辑判断,：
        boolean b = studentService.addStudent(student);
        if (b) {
            System.out.println("添加成功");
        } else {
            System.out.println("添加失败");
        }


    }

    public final void searchStudent() {
        ArrayList<Student> list = studentService.findAllStudent();
        if (list == null || list.isEmpty()) {
            System.out.println("暂无学生信息，请添加");
            return;
        }
        System.out.println("工号\t\t姓名\t\t年龄\t\t生日"); // \t 等同于4个空格
        for (Student student : list) {
            if (student != null) {
                System.out.println(student.getId() + "\t\t" + student.getName() + "\t\t" + student.getAge() + "\t\t" + student.getBirthday());
            } else
                System.out.println(student);
        }
    }

    private void updateStudent() {

        //输入需要修改的学生id:
        System.out.println("请输入需要修改的学生id:");
        String id = sc.next();
        //提交给业务员判断：
        boolean exists = studentService.isExists(id);
        //如果exists为flase：不存在：
        if (exists == false) {
            System.out.println("您输入的学号不存在");
            return;
        }

        //录入学生信息：：
        Student student = inputStudentInfo(id);


        studentService.updateStudent(student);
        System.out.println("学生修改成功");

    }

    private final void deleteStudent() {
        //输入需要删除的学生id:
        System.out.println("请输入需要删除的学生id:");
        String id = sc.next();
        //提交给业务员判断：
        boolean exists = studentService.isExists(id);
        //如果exists为flase：不存在：
        if (exists == false) {
            System.out.println("您输入的学号不存在");
            return;
        } else {
            studentService.deleteStudent(id);

            System.out.println("删除成功");
        }

    }


    //年龄输入前加判断的方法：
    public int getAge() {
        int age;
        while (true) {
            System.out.println("请输入学生年龄：");
            age = sc.nextInt();
            //对年龄进行逻辑判断：
            boolean flag = studentService.anlayzAge(age);
            if (flag == false) {
                System.out.println("您输入的年龄超出常态，范围：0-140;请重新输入：");
            } else {
                break;
            }
        }
        return age;
    }

    //录入学生信息：
    public abstract Student inputStudentInfo(String id);
}


